using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Xpo;
using Component;

namespace Component
{
    public class NewsDB : Base
    {    
        /// <summary>
        /// public static XPCollection<News> GetAll()
        /// </summary>
        /// <returns>XPCollection<News></returns>
        public static XPCollection<News> GetAll(bool admin)
        {
            XPCollection<News> _xpNewss = new XPCollection<News>(KetNoiCSDL.session);
            _xpNewss.Filter = FilterAll(admin);
            SortingCollection sort =new SortingCollection ();
            sort .Add (new SortProperty("Date",DevExpress.Xpo.DB.SortingDirection.Descending));
            _xpNewss.Sorting = sort;
            return _xpNewss;
        }
        /// <summary>
        /// public static News GetObjectByid(int id)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static News GetObjectByid(int id,bool admin)
        {
            XPCollection<News> _xpNewss = new XPCollection<News>(KetNoiCSDL.session);
            _xpNewss.Filter = FilterAll(admin);
            return _xpNewss.Lookup(id);
        }
        /// <summary>
        ///       public static XPCollection<News> GetAllNews(bool admin)
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public static XPCollection<News> GetAllNews(bool admin)
        {
            XPCollection<News> _xpNewss = new XPCollection<News>(KetNoiCSDL.session);
            if (admin == false)
            {
                 _xpNewss.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumNews._Active + ConstString._dauBangVaAnd + ColumNews._New + ConstString._dauBang, true, true);
            }
            else
                _xpNewss.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumNews._New + ConstString._dauBang, true);
            return _xpNewss;
        }
      /// <summary>
        ///     public static XPCollection<News> GetAllServices(bool admin)
      /// </summary>
      /// <param name="admin"></param>
      /// <returns></returns>
        public static XPCollection<News> GetAllServices(bool admin)
        {
            XPCollection<News> _xpNewss = new XPCollection<News>(KetNoiCSDL.session);
            if (admin == false)
            {
                _xpNewss.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumNews._Active + ConstString._dauBangVaAnd + ColumNews._Service + ConstString._dauBang, true,true);
            }
            else
                _xpNewss.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumNews._Service + ConstString._dauBang, true);
            return _xpNewss;
        }
      
        /// <summary>
        ///     public static XPCollection<News> GetAllServices(bool admin)
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public static XPCollection<News> GetFiveService(bool admin)
        {
            XPCollection<News> _xpNewss = new XPCollection<News>(KetNoiCSDL.session);
            if (admin == false)
            {
                _xpNewss.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumNews._Active + ConstString._dauBangVaAnd + ColumNews._Service + ConstString._dauBang, true, true);
                _xpNewss.Sorting = SetupSort(ColumNews._Date, false);
                int tam = _xpNewss.Count;

                for (int i = tam - 1; i >=5; i--)
                { _xpNewss.Remove(_xpNewss[i]); }

            }
            else
                _xpNewss.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse(ColumNews._Service + ConstString._dauBang, true);
           
            return _xpNewss;
        }
        public static string GetContentByLanguage(string columName, News news)
        {
            switch (columName)
            {
                case ColumNews._Titile:
                    {
                        if (LanguageVN == true)
                            return news.Titile;
                        return news.TitileEL;
                    }
                case ColumNews._Summary:
                    {
                        if (LanguageVN == true)
                            return news.Summary;
                        return news.SummaryEL;
                    }
                case ColumNews._Content:
                    {
                        if (LanguageVN == true)
                            return news.Content;
                        return news.ContentEL;
                    }
            }
            return "";
        }
      
    }
}
